import Vue from 'vue'
import Router from 'vue-router'
import {isAuthenticated} from '../utils/auth'
Vue.use(Router)

export const constantRouterMap = [
  { path: '/',
    name:'home',
   component: () => import('@/views/fowardindex/index')
  },
  { path: '/login',
    name: 'login',
   component: () => import('@/views/login/index')
  },
  { path: '/regist',
    name:'regist' ,
  component: () => import('@/views/regist/index')
},
  { path: '/book',
   name: 'BookDetail', 
   component: () => import('@/views/detail/index')
  },
  { path: '/car', 
   name:'Car',
  component: () => import('@/views/car/index'),
  beforeEnter: (to, from, next) => {
    // ...
    if(to.name !== 'login' && !isAuthenticated()){
      next({name:'login'})
    }
    else{
      next()
    }
  }
 },
  { path: '/beforecreorder', 
    name:'BeforeCreO',
  component: () => import('@/views/order/beforecreate'),
  beforeEnter: (to, from, next) => {
    // ...
    if(to.name !== 'login' && !isAuthenticated()){
      next({name:'login'})
    }
    else{
      next()
    }
  }
  },
  { path: '/orderlist',
   name:'Orderlist', 
  component: () => import('@/views/order/orderlist'),
  beforeEnter: (to, from, next) => {
    // ...
    if(to.name !== 'login' && !isAuthenticated()){
      next({name:'login'})
    }
    else{
      next()
    }
  }
 },
  { path: '/pay',
    name:'Pay',
   component: () => import('@/views/pay/index'),
   beforeEnter: (to, from, next) => {
    // ...
    if(to.name !== 'login' && !isAuthenticated()){
      next({name:'login'})
    }
    else{
      next()
    }
  } 
  },
  { path: '*',
   redirect: '/404', hidden: true 
  }
  ]
  
  export default new Router({
    // mode: 'history', //后端支持可开
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  })
  